package cn.itcast.aop;

import java.lang.reflect.Method;

import cn.itcast.service.PersonService;
import cn.itcast.service.impl.PersonServiceBean;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CGlibProxyFactory implements MethodInterceptor{
	private Object targetObject;
	
	public Object createTargetInstance(Object targetObject) {
		this.targetObject = targetObject;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(this.targetObject.getClass());
		enhancer.setCallback(this);
		return enhancer.create();
		
	}

	@Override
	public Object intercept(Object arg0, Method arg1, Object[] args,
			MethodProxy methodProxy) throws Throwable {
		// TODO Auto-generated method stub
		PersonServiceBean bean = (PersonServiceBean) this.targetObject;
		Object result = null;
		if(bean.getUser() != null) {
			result = methodProxy.invoke(this.targetObject, args);
			
		}
		return result;
	}
}
